<template>
  <div>
    <div style="margin-top: 10px; margin-left: 10px">
      <el-button type="primary" @click="showAddDialog">添加歌手</el-button>
      <el-table :data="tableData" style="width: 100%">
        <el-table-column label="姓名" width="180" prop="name">
        </el-table-column>
        <el-table-column label="性别" width="180">
          <template slot-scope="scope">
            <p>{{ scope.row.sex }}</p>
          </template>
        </el-table-column>
        <el-table-column label="国籍" width="180" prop="country">
        </el-table-column>
        <el-table-column label="图像" width="180" prop="img1v1Url">
        </el-table-column>
        <el-table-column label="日期" width="180" prop="createTime">
        </el-table-column>

        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button size="mini" @click="handleEdit(scope.$index, scope.row)"
              >编辑</el-button
            >
            <el-button
              size="mini"
              type="danger"
              @click="handleDelete(scope.$index, scope.row)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="artistReq.page"
        :page-sizes="[1, 3, 5, 10]"
        :page-size="artistReq.size"
        layout="sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>

      <el-dialog title="添加演员" :visible.sync="dialogFormVisible">
        <el-form :model="artist" label-position="left" label-width="80px">
          <el-form-item label="演员名字">
            <el-input v-model="artist.name" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="演员国籍">
            <el-input v-model="artist.country" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="性别">
            <el-select v-model="artist.sex" placeholder="">
              <el-option label="男" value="0"></el-option>
              <el-option label="女" value="1"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="上传图片"    v-if="selectId == -1">
            <el-upload
        
              class="avatar-uploader"
              action="https://jsonplaceholder.typicode.com/posts/"
              :show-file-list="false"
              :on-success="handleAvatarSuccess"
              :before-upload="beforeAvatarUpload"
            >
              <img v-if="imageUrl" :src="artist.img1v1Url" class="avatar" />
              <i v-else class="el-icon-plus avatar-uploader-icon"></i>
            </el-upload>
          </el-form-item>
          <el-form-item label="图片" v-if="artist.img1v1Url">
            <el-image
              style="width: 100px; height: 100px"
              :src="artist.img1v1Url"
              :fit="fit"
            ></el-image>
          </el-form-item>
        </el-form>

        <div slot="footer" class="dialog-footer">
          <el-button @click="hideDialog">取 消</el-button>
          <el-button type="primary" @click="saveArtist">保存</el-button>
        </div>
      </el-dialog>
    </div>
  </div>
</template>
<style>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>
  <script>
import artistApi from "@/api/artist.js";
export default {
  data() {
    return {
      artistReq: {
        page: 1,
        size: 3,
      },
      total: 2,
      tableData: [],
      dialogFormVisible: false,
      artist: {},
      selectId: -1,
    };
  },
  created() {
    this.getData();
  },
  methods: {
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
      this.artistReq.size = val;
      this.getData();
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.artistReq.page = val;
      this.getData();
    },
    handleEdit(index, row) {
      console.log(index, row);
      this.selectId = row.id;
      this.getInfoById()
    },
    handleDelete(index, row) {
      console.log(index, row);
    },

    getData() {
      artistApi.getArtistList(this.artistReq).then((r) => {
        console.log(r);
        if (r.code == 0) {
          this.tableData = r.data.page.records;
          this.total = Number(r.data.page.total);
        }
      });
    },
    showAddDialog() {
      //   alert("添加");
      this.dialogFormVisible = true;
    },
    hideDialog() {
      this.selectId = -1;
      this.dialogFormVisible = false;
    },
    saveArtist() {
      this.dialogFormVisible = false;
      this.selectId = -1;
      artistApi.addArtist(this.artist).then((r) => {
        console.log(r);
        if (r.code == 0) {
          this.$message.success(r.message);
        }
      });
    },
    getInfoById() {
        artistApi.findById(this.selectId).then((r) => {
        console.log(r);
        if (r.code == 0) {
          this.artist = r.data.data;
         this.showAddDialog();
        }
      });
    },
  },
};
</script>